clear all
global codepath "/Users/xiangfang/Dropbox/Project/Sovereign Bond Project/Replication_Package_submission_RFS/Code"
global datapath "/Users/xiangfang/Dropbox/Project/Sovereign Bond Project/Replication_Package_submission_RFS/Data/Paper_Data"
global outTEX "/Users/xiangfang/Dropbox/Project/Sovereign Bond Project/Replication_Package_submission_RFS/Tables"
cd "$codepath"

include "${codepath}/iv_construction_em.do"

cap confirm file "${outTEX}"
if _rc != 0 {
	cap mkdir "${outTEX}"
}
use "${datapath}/holding_em.dta", clear

drop if mi(real_fx)
drop if country == "Venezuela"
******************* Expected Excess Returns *****************************
bys country (year): gen yield_5y_lag = yield_5y[_n-1]
bys country (year): gen short_rate_lag = short_rate[_n-1]
bys country (year): gen p_5y_lag = p_5y[_n-1]
bys country (year): gen l_real_fx_lag = l_real_fx[_n-1]
order country year yield_5y yield_5y_lag short_rate short_rate_lag l_real_fx l_real_fx_lag 
gen l_p_5y = ln(p_5y)
gen l_p_5y_lag = ln(p_5y_lag)

// holding period returns hpr_t = -(T-1)y_t^(T-1)+T*y_{t-1}^T\approx -(T-1)y_t^(T)+T*y_{t-1}^T
rename dom_ex_ret_5y dom_exret
rename for_ex_ret_5y for_exret 

// Domestic expected excess returns
reghdfe dom_exret l_p_5y_lag, absorb(dom_country_FE_ex = country_id) noconstant // include country FE
display
est store m1
local coef_yield = e(b)[1,1]
bys country (year): replace dom_country_FE_ex = dom_country_FE_ex[_n+1] if dom_country_FE_ex==.
cap gen dom_exp= `coef_yield' * l_p_5y +  dom_country_FE_ex
winsor dom_exp,gen(dom_exp1) p(0.01)

// Foreign expected excess returns
eststo: reghdfe for_exret l_real_fx_lag l_p_5y_lag, absorb(for_country_FE_ex = country_id) noconstant
est store m2
local coef_rer = e(b)[1,1]
local coef_yield = e(b)[1,2]

bys country (year): replace for_country_FE_ex = for_country_FE_ex[_n+1] if for_country_FE_ex==.
cap gen for_exp = `coef_rer' * l_real_fx + `coef_yield' *  l_p_5y + for_country_FE_ex
winsor for_exp,gen(for_exp1) p(0.01)


// Bond price and exchange rate

******************* Instrument Construction *****************************
// Preparations
gen l_total_usd = ln(Total)

by country: gen lag_total = l_total_usd[_n-1]
by country: gen lag_ltot_gdp = l_tot_gdp[_n-1]
by country: gen lag_tot_gdp = Total[_n-1]/gdp[_n] // What we want is (D_t-a*D_t-1)/GDP_t depends on characteristics


generate_iv_ols_supply, instrument_var(iv) xvars(l_gdp real_gdp_growth inflation l_exp_ratio rate_num_loc) fixedeffects(i.country_id i.year) yr(5)


winsor yield_5y,generate(yield_5y1) p(0.01) // winsorize yield
winsor iv_l_price, gen(iv_l_price1) p(0.01)

******************* Regression Specifications: demand estimation ***************************
eststo clear
foreach x in DomBK DomNB DomCB{
	eststo: ivreghdfe l_`x'_mkt l_gdp real_gdp_growth inflation l_exp_ratio rate_num_loc (dom_exp1 = iv_l_price1), absorb (country_id year) noconstant
}

foreach x in ForBK ForNB ForCB{
	eststo: ivreghdfe l_`x'_mkt l_gdp real_gdp_growth inflation l_exp_ratio rate_num_loc (for_exp1 = iv_l_price1), absorb (country_id year) noconstant
}
esttab est1 est2 est3 est4 est5 est6 using "${outTEX}/Table4.tex", replace b(%5.3f) se(%5.3f) label booktabs star(* .1 ** .05 *** .01)  nogaps noomit nogaps nobase ///
order(dom_exp1 for_exp1 l_gdp real_gdp_growth inflation l_exp_ratio rate_num_loc) ///
	title("Demand Estimation: Instrumental Variable Estimate") ///
	mtitles( "\shortstack{DomBK}" "\shortstack{DomNB}" "\shortstack{DomCB}" "\shortstack{ForBK}" "\shortstack{ForNB}" "\shortstack{ForCB}" "\shortstack{Total}" ) nonotes
